﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;

namespace RingSong
{
    public class Anm
    {
        public static List<object> isShowBtns = new List<object>();
        public static List<object> isShowPro = new List<object>();
        public static List<object> isShowWait = new List<object>();
        //显示和隐藏
        public static void Show_Hide(object o, string objName, bool isShow,ListBox list)
        {
            if (o == null) return;
            Storyboard sto = new Storyboard();
            ListBoxItem item = list.ItemContainerGenerator.ContainerFromItem(o) as ListBoxItem;
            if (item == null) return;
            StackPanel pan = Com.FindFirstElementInVisualTree<StackPanel>(item);
            Grid grd = Com.GetChildObject<Grid>(pan, "grd");
            Grid pro = Com.GetChildObject<Grid>(grd, "pro");
            Grid btns = Com.GetChildObject<Grid>(grd, "btns");
            Grid wait = Com.GetChildObject<Grid>(grd, "wait");

            DoubleAnimation da = new DoubleAnimation() { Duration = TimeSpan.FromMilliseconds(300) };
            DoubleAnimation da2 = new DoubleAnimation() { Duration = TimeSpan.FromMilliseconds(150) };

            Storyboard.SetTargetProperty(da, new PropertyPath("(UIElement.RenderTransform).(CompositeTransform.TranslateY)"));
            Storyboard.SetTargetProperty(da2, new PropertyPath("(FrameworkElement.Height)"));

            #region 位置调整
            if (objName == "pro")
            {
                pro.Visibility = Visibility.Visible;
                btns.Visibility = wait.Visibility = Visibility.Collapsed;
                if (isShow)
                {
                    grd.Margin = new Thickness(0, -14, 0, 0);
                    da.To = 14; da2.To = 18;
                }
                else
                {
                    grd.Margin = new Thickness(0, 0, 0, 0);
                    da.To = 0; da2.To = 0;
                }
            }
            else if (objName == "btns")
            {
                btns.Visibility = Visibility.Visible;
                pro.Visibility = wait.Visibility = Visibility.Collapsed;

                if (isShow)
                {
                    da.To = 15; da2.To = 71;
                }
                else
                {
                    da.To = 0; da2.To = 0;
                }
            }
            else if (objName == "wait")
            {
                wait.Visibility = Visibility.Visible;
                pro.Visibility = btns.Visibility = Visibility.Collapsed;

                if (isShow)
                {
                    da.To = 60; da2.To = 33;
                }
                else
                {
                    da.To = 0; da2.To = 0;
                }
            }

            #endregion
            Storyboard.SetTarget(da, grd);
            Storyboard.SetTarget(da2, grd);
            sto.Completed += (_, __) =>
            {
                if (isShow)
                {
                    if (objName == "pro")
                        isShowPro.Add(o);
                    else if (objName == "btns")
                        isShowBtns.Add(o);
                    else if (objName == "wait")
                        isShowWait.Add(o);
                }
                else
                {
                    if (objName == "pro")
                        isShowPro.Remove(o);
                    else if (objName == "btns")
                        isShowBtns.Remove(o);
                    else if (objName == "wait")
                        isShowWait.Remove(o);
                }
            };
            if (objName != "wait")  sto.Children.Add(da); 
            //sto.Children.Add(da); 
            sto.Children.Add(da2);
            sto.Begin();
        }

        //检测1个是否有开启，有则关闭
        public static void CloseOne(object o, ListBox lst)
        {
            if (isShowBtns.Contains(o))
                Show_Hide(o,"btns",false,lst);
            if (isShowPro.Contains(o))
                Show_Hide(o, "pro", false, lst);
            if (isShowWait.Contains(o))
                Show_Hide(o, "wait", false, lst);
        }

        //检测除了Btns外的其他两个是否有开启的，若一个有则返回false
        public static bool CheckBtns(object o,ListBox lst)
        {
            if (isShowPro.Contains(o) || isShowWait.Contains(o))
                return false;
            else
                return true;
        }

    }
}
